angular.module('liemallfodder')
  .controller('MaterialListCtrl', function ($scope, $rootScope, $stateParams, Toast, Loading, ApiManager, MaterialApi) {

    $scope.materials = [];

    var requestListMaterial = function () {
      var subcategory_id = $stateParams.subcategory_id;
      if (subcategory_id < 0) {
        Toast.show('错误的id');
        return;
      }
      Loading.show('正在加载');
      MaterialApi.listMaterial(subcategory_id, function (result) {
        Loading.hide();
        if (ApiManager.hasError(result)) {
          return;
        }
        $scope.materials = result.data;

      }, function (error) {
        Loading.hide();
        ApiManager.showError(error);
      })
    };

    $scope.getItemUrl = function (imageUrl) {
      var url = 'img/ic_logo@2x.png';
      if (imageUrl !== undefined && imageUrl.length > 0) {
        url = $rootScope.toAbsoluteUrl(imageUrl);
      }
      return url;
    };

    requestListMaterial();

  });
